﻿/****************************** Module Header ******************************\
 * Module Name:  TreeListComboBox.cs
 * Module Version:  2.0.0 (2012-2-7)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This componse is use for pick the catalog.
 * Note:已弃用,推荐使用 JqCombo控件
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UCF.Core.Web.CustomControls
{
    /// <summary>
    /// 树形列表下拉控件
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TreeListComboBox runat=server></{0}:TreeListComboBox>")]
    public class TreeListComboBox : CompositeControl
    {
        /// <summary>
        /// 值
        /// </summary>
        public string Value
        {
            get
            {
                EnsureChildControls();
                return this.hidValue.Value;
            }
            set
            {
                EnsureChildControls(); this.hidValue.Value = value;
            }
        }

        /// <summary>
        /// 文本
        /// </summary>
        public string Text
        {
            get
            {
                EnsureChildControls(); return this.tbText.Text;
            }
            set
            {
                EnsureChildControls(); this.tbText.Text = value;
            }
        }

        /// <summary>
        /// 数据
        /// </summary>
        public string DataContent { get; set; }

        /// <summary>
        /// 控制选项
        /// </summary>
        public string Options { get; set; }

        /// <summary>
        /// 普通下拉选项框的通用编号
        /// </summary>
        public string TLCTextGeneralID
        {
            get
            {
                String s = (String)ViewState["TLCTextGeneralID"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["TLCTextGeneralID"] = value;
            }
        }

        /// <summary>
        /// 隐藏文本域的通用编号
        /// </summary>
        public string TLCHiddenGeneralID
        {
            get
            {
                String s = (String)ViewState["TLCHiddenGeneralID"];
                return ((s == null) ? String.Empty : s);
            }
            set
            {
                ViewState["TLCHiddenGeneralID"] = value;
            }
        }

        private TextBox tbText;
        private HiddenField hidValue;

        /// <summary>
        /// 创建子控件
        /// </summary>
        protected override void CreateChildControls()
        {
            this.TLCHiddenGeneralID = "TLC" + this.ID + this.UniqueID.Replace('$', '_') + "hidValue";
            this.TLCTextGeneralID = "TLC" + this.ID + this.UniqueID.Replace('$', '_');
            this.tbText = new TextBox();
            this.hidValue = new HiddenField();
            this.tbText.ID = this.TLCTextGeneralID;
            this.hidValue.ID = this.TLCHiddenGeneralID;
            this.hidValue.ClientIDMode = ClientIDMode.Static;
            this.tbText.ClientIDMode = ClientIDMode.Static;
            if (this.CssClass != null) tbText.CssClass = this.CssClass;
            this.Controls.Add(this.tbText);
            this.Controls.Add(this.hidValue);
        }

        /// <summary>
        /// 预绘制处理
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            Page.PreRenderComplete += PagePreRenderComplete;
            base.OnPreRender(e);
        }

        /// <summary>
        /// 页面重绘完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void PagePreRenderComplete(object sender, EventArgs e)
        {
            if (this.DataContent == null) return;
            string sScript = "(function($){$.fn.extend({'DropDownControl':function(options){if(!$(this)[0])return;options=$.extend({value:'请选择分类 ∨',data:'',style:{'display':'none','position':'absolute','background':'#FFF','border':'solid 1px #CCC','padding':'0px 10px 10px 10px','line-height':'21px','z-index':'999'},height:'',width:'',valueInputID:'',sublevMarginLeft:'20',debug:false},options);if(!options.data){options.data='<div><div class=\"lev1\"><label id=\"'+options.value+'\">请选择分类 ∨</label></div>'}var o={};o.$e=$(this);if(!options.valueInputID)options.valueInputID=o.$e.attr('id')+'hidValue';o.$ev=$('#'+options.valueInputID);o.$data=$(options.data).prepend('<div><a href=\"###\" title=\"收起所有二级分类\">&lt;&lt;&lt;</a> <a href=\"###\" title=\"收起所有三级分类\">&lt;&lt;</a></div>');o.$data.css(options.style);if(options.sublevMarginLeft)o.$data.find('.lev').css('margin-left',options.sublevMarginLeft);o.$data.find('label').css({'display':'inline','margin':'0','padding':'0'});o.$data.find('.lev1>label').css('font-weight','bold');o.$data.find('.lev2>label').css('color','#09C');o.$data.find('.v').css({'margin-left':'10','color':'#F60'});if(options.height){o.$data.css({'height':options.height,'overflow-y':'auto'})};if(options.width)o.$data.css('width',options.width);o.SetValue=function($o,v){if(!$o[0])return;($o[0].tagName=='INPUT')?$o.val(v):$o.text(v)};o.GetValue=function($o){return($o[0].tagName=='INPUT')?$o.val():$o.text()};o.$item=o.$data.find('label[id=\"'+options.value+'\"]').css({'display':'inherit','background-color':'#F2F2F2','font-weight':'bold','color':'#090'});if(o.$item[0]){o.SetValue(o.$e,o.$item.text());o.SetValue(o.$ev,o.$item.attr('id'))}else{o.SetValue(o.$e,options.value)};o.$e.click(function(){o.$e.after(o.$data).wrap('<div></div>').css({'position':'relative'});o.$data.toggle();o.$data.scrollTop(o.$data.offset().top)});o.$data.find('label').css('cursor','pointer').click(function(){o.SetValue(o.$e,$(this).text());o.SetValue(o.$ev,$(this).attr('id'));o.$data.hide()});o.$data.find('a.v').click(function(){o.$me=$(this);if(o.$me.text()=='[+]'){o.$me.text('[-]');o.$me.nextAll('div').show()}else{o.$me.text('[+]');o.$me.nextAll('div').hide()}});o.$Dis=o.$data.find(':first a');o.$Dis.eq(0).click(function(){o.$a=$(this);if(o.$a.text()=='>>>'){o.$a.text('<<<');o.$a.attr('title','收起所有二级分类');o.$data.find('.lev2').show();o.$data.find('.lev1 .v').text('[-]')}else{o.$a.text('>>>');o.$a.attr('title','展开所有二级分类');o.$data.find('.lev1 .v').text('[+]');o.$data.find('.lev2').hide()}});o.$Dis.eq(1).click(function(){o.$a=$(this);if(o.$a.text()=='>>'){o.$a.text('<<');o.$a.attr('title','收起所有三级分类');o.$data.find('.lev2 .v').text('[-]');o.$data.find('.lev3').show()}else{o.$a.text('>>');o.$a.attr('title','展开所有三级分类');o.$data.find('.lev2 .v').text('[+]');o.$data.find('.lev3').hide()}})}})})(jQuery);";
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "TreeListComboBox", sScript, true);

            if (this.Options == null) this.Options = "height:'240',width:'160'";
            if (this.Options.Length > 0) this.Options.Trim(',');
            if (this.Value != null && this.Value.Length > 0) this.Options += ",value:'" + this.Value + "'";
            if (this.Options.Length > 0) this.Options.Trim(',');
            if (this.Options.Length > 0) this.Options += ",";
            string runScript = "jQuery(document).ready(function($){$('#" + this.TLCTextGeneralID + "').DropDownControl({" + this.Options + "data:'" + this.DataContent + "'})});";
            this.Page.ClientScript.RegisterStartupScript(typeof(Page), "tlcb" + this.UniqueID.Replace('$', '_'), runScript, true);
        }

        /// <summary>
        /// 重绘控件外观
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            this.tbText.RenderControl(writer);
            this.hidValue.RenderControl(writer);
        }
    }
}
